/*
========================================================================
 Name        : ExpensesForm.cpp
 Author      : Vishal Dharankar
 Copyright   : Created by Vishal Dharankar 
 Description : 
========================================================================
*/
// [[[ begin generated region: do not modify [Generated System Includes]
#include <avkon.hrh>
#include <avkon.rsg>
#include <eikmenup.h>
#include <aknappui.h>
#include <eikcmobs.h>
#include <barsread.h>
#include <stringloader.h>
#include <gdi.h>
#include <eikmfne.h>
#include <eikenv.h>
#include <eikfpne.h>
#include <aknpopupfieldtext.h>
#include <eikedwin.h>
#include <Expenses.rsg>
// ]]] end generated region [Generated System Includes]
#include "ExpensesAppUi.h"
// [[[ begin generated region: do not modify [Generated User Includes]
#include "ExpensesForm.h"
#include "Expenses.hrh"
#include "ExpensesListBox.hrh"
#include "ExpensesForm.hrh"
// ]]] end generated region [Generated User Includes]

// [[[ begin generated region: do not modify [Generated Constants]
_LIT( KFloatingPointEditorExpTextValue, "0" );
// ]]] end generated region [Generated Constants]

/**
 * Construct the CExpensesForm instance
 * @param aCommandObserver command observer
 */ 
CExpensesForm::CExpensesForm( MEikCommandObserver* aCommandObserver )
	{
	iCommandObserver = aCommandObserver;
	// [[[ begin generated region: do not modify [Generated Contents]
	iDateEditor = NULL;
	iFloatingPointEditorExp = NULL;
	iPopupFieldTextCat = NULL;
	iEditNote = NULL;
	// ]]] end generated region [Generated Contents]
	
	}

/**
 *  Creates an instance and initializes it.
 *  Instance is not left on cleanup stack.
 * @param aCommandObserver command observer
 * @return initialized instance of CExpensesForm
 */
CExpensesForm* CExpensesForm::NewL( MEikCommandObserver* aCommandObserver )
	{
	CExpensesForm* self = CExpensesForm::NewLC( aCommandObserver );
	CleanupStack::Pop( self );
	return self;
	}

/**
 *  Creates an instance and initializes it.
 *  Instance is left on cleanup stack.
 * @param aCommandObserver command observer
 * @return new instance of CExpensesForm
 */
CExpensesForm* CExpensesForm::NewLC( MEikCommandObserver* aCommandObserver )
	{
	CExpensesForm* self = new ( ELeave ) CExpensesForm( aCommandObserver );
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
	}
			
/** 
 * Handle key event (override)
 * @param aKeyEvent key event
 * @param aType event code
 * @return EKeyWasConsumed if the event was handled, else EKeyWasNotConsumed
 */
TKeyResponse CExpensesForm::OfferKeyEventL( 
		const TKeyEvent& aKeyEvent, 
		TEventCode aType )
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	if ( ( aKeyEvent.iCode == EKeyLeftArrow 
		|| aKeyEvent.iCode == EKeyRightArrow )
		&& !IsEditable() )
		{
		// allow the tab control to get the arrow keys
		return EKeyWasNotConsumed;
		}
	
	return CAknForm::OfferKeyEventL( aKeyEvent, aType );
	}
				
/** 
 * Destroy any instance variables
 */
CExpensesForm::~CExpensesForm()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	}

/**
 * Called to handle "Save" menu item. Displays save query.
 * @return TBool ETrue if the form data is to be saved, EFalse otherwise
 */
TBool CExpensesForm::QuerySaveChangesL()
	{
	TBool isAnsYes( CAknForm::QuerySaveChangesL() );
	
	if ( isAnsYes )
		{
		SaveFormDataL();
		}
	else
		{
		DoNotSaveFormDataL();
		}

	return isAnsYes;
	}

/**
 * Called from QuerySaveChangesL when changes made to the form are to be saved.
 * @return TBool ETrue if the form data has been saved, EFalse otherwise
 */
TBool CExpensesForm::SaveFormDataL()
	{
	// TODO save values
	CExpensesAppUi * ui = (CExpensesAppUi*)CEikonEnv::Static()->AppUi();
	
	RArray<Expense>& array = ui->ExpArray();
	TTime date = this->iDateEditor->Date();
	TReal exp = this->iFloatingPointEditorExp->Value();
	TBuf<50> note;
	this->iEditNote->GetText(note);
	TInt cat = this->iPopupFieldTextCat->CurrentValueIndex();
	

	Expense expObj;
	expObj.SetCategory(cat);
	expObj.SetDate(date);
	expObj.SetExp(exp);
	expObj.SetNote(note);
	
	array.Append(expObj);
	
	
	
	ui->ActivateLocalViewL(TUid::Uid(EExpensesListBoxViewId));
	return ETrue;
	}

/**
 * Called from QuerySaveChangesL when changes made to the form are discarded.
 */
void CExpensesForm::DoNotSaveFormDataL()
	{
	LoadFromDataL();
	}

/**
 * Called from DoNotSaveFormDataL when changes are cancelled.
 * Called from PreLayoutDynInitL to load initial values if needed.
 */
void CExpensesForm::LoadFromDataL()
	{
	// TODO load saved values	
	CExpensesAppUi * ui = (CExpensesAppUi*)CEikonEnv::Static()->AppUi();
	RArray<Expense>& array = ui->ExpArray();
	if(array.Count() < 1)
		return;
	
	if(ui->GetAddOrView() == false)
		{
			TInt selIndex = ui->GetSelIndex();
			
			TTime date = array[selIndex].GetTime();
			TReal exp = array[selIndex].GetExp();
			TInt cat;
			cat = array[selIndex].GetCategory();
			TBuf<20> note;
			array[selIndex].GetNote(note);
			
			// load to controls
			this->iFloatingPointEditorExp->SetValueL(&exp);
			this->iEditNote->SetTextL(&note);
			this->iPopupFieldTextCat->SetCurrentValueIndex(cat);
			this->iDateEditor->SetDate(date);
		}
	else
		{
		   
					
			TTime date ;
			date.HomeTime();
			TReal exp = 0;
			TInt cat=0;
			TBuf<20> note;
			note=_L("");
			this->iFloatingPointEditorExp->SetValueL(&exp);
			this->iEditNote->SetTextL(&note);
			this->iPopupFieldTextCat->SetCurrentValueIndex(cat);
			this->iDateEditor->SetDate(date);
			
			
		}
	
	
	}

/**
 * Initialize controls and settings before a Form is laid out.  (override)
 */
void CExpensesForm::PreLayoutDynInitL()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	iDateEditor = static_cast< CEikDateEditor* >( 
		ControlOrNull( EExpensesFormViewDateEditor ) );
	iDateEditor->SetDate( TTime( TDateTime( 2000, EJanuary, 0, 0, 0, 0, 0 ) ) );
	iFloatingPointEditorExp = static_cast< CEikFloatingPointEditor* >( 
		ControlOrNull( EExpensesFormViewFloatingPointEditorExp ) );
		{
		TReal value = 0;
		iFloatingPointEditorExp->SetValueL( &value );
		iFloatingPointEditorExp->SetTextL( &KFloatingPointEditorExpTextValue );
		}
	iPopupFieldTextCat = static_cast< CAknPopupFieldText* >( 
		ControlOrNull( EExpensesFormViewPopupFieldTextCat ) );
	iPopupFieldTextCat->SetFont( iEikonEnv->NormalFont() );
	iEditNote = static_cast< CEikEdwin* >( 
		ControlOrNull( EExpensesFormViewEditNote ) );
		{
		HBufC* text = StringLoader::LoadLC( R_EXPENSES_FORM_EDIT_NOTE );
		iEditNote->SetTextL( text );
		CleanupStack::PopAndDestroy( text );
		}
	// ]]] end generated region [Generated Contents]
	
	LoadFromDataL();
	}
				
/**
 * Called when built-in menu is displayed.  (override)
 * CAknForm has a built-in menu. In view mode, the only menu item is "Edit" that switches the form to edit mode.
 * In edit mode, the items are "Add field" "Save" "Edit label" and "Delete field"
 * The code below removes all edit mode items except for "Save"
 * To use these items, remove the appropriate lines and override the appropriate CAknForm methods.
 * (E.g., to handle "Add field" remove the line for EAknFormCmdAdd and override CAknForm::AddItemL())
 */
void CExpensesForm::DynInitMenuPaneL( TInt aResourceId, CEikMenuPane* aMenuPane )
	{
	CAknForm::DynInitMenuPaneL( aResourceId, aMenuPane );
	
	if ( aResourceId == R_AVKON_FORM_MENUPANE )
		{
		aMenuPane->SetItemDimmed( EAknFormCmdAdd, ETrue );
		aMenuPane->SetItemDimmed( EAknFormCmdLabel, ETrue );
		aMenuPane->SetItemDimmed( EAknFormCmdDelete, ETrue );

		/* 
		// sample code to add a custom item with command id 'myCommandId'
		TInt pos;
		if ( !aMenuPane->MenuItemExists( myCommandId, pos ) )
			{
			CEikMenuPaneItem::SData menuItem;
			menuItem.iCommandId = myCommandId;
			menuItem.iCascadeId = 0;	// set for submenus
			menuItem.iFlags = 0;		// e.g. EEikMenuItemDimmed
			menuItem.iText = KMyCommandIdText;
			menuItem.iExtraText = _L("");
			aMenuPane->AddMenuItemL( menuItem );
			}
		*/
		}
	// [[[ begin generated region: do not modify [Generated Code]
	
	// ]]] end generated region [Generated Code]
	
	}
				
/**
 * Handle a button press and tell whether it closes the dialog.  (override)  
 * @param aButtonId the identifier for the button (avkon.hrh)
 */
TBool CExpensesForm::OkToExitL( TInt aButtonId )
	{
		
	// [[[ begin generated region: do not modify [Generated Code]
	
	// ]]] end generated region [Generated Code]
	
	// The 'exit application on close' property automatically adds code to exit the 
	// application when the right soft key is pressed. If this property is set to false, 
	// and AddToStackL has been called on the Form object (e.g., it was not shown as a dialog), 
	// code must be added manually to avoid a panic when exiting the application.
	
	if(aButtonId == EAknSoftkeyBack)
		{
		//CExpensesAppUi * ui = (CExpensesAppUi*)CEikonEnv::Static()->AppUi();
		//	ui->ActivateLocalViewL(TUid::Uid(EExpensesListBoxViewId));	
		iCommandObserver->ProcessCommandL(EAknCmdExit);
		return EFalse;


		}
	
	TBool res = CAknForm::OkToExitL( aButtonId );
//	CCoeEnv::Static()->AppUi()->RemoveFromStack(this);
	return res;
	
	}
void CExpensesForm::InitForm()
	{
	//TBuf<2> temp;
	//temp = _L("");
	//this->iEditNote->SetTextL(&temp);
	}
